package net.javaw.weather;

import java.io.UnsupportedEncodingException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	private static final String NAMESPACE = "http://WebXml.com.cn/";  
	// WebService地址 	后面的？wsdl根据WebService地宫的地址可加可不加
	private static String URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";  
	private static final String METHOD_NAME = "getWeatherbyCityName";  
	private static String SOAP_ACTION = NAMESPACE + METHOD_NAME;  

	private String weatherToday;  

	private Button okButton;  
	private SoapObject detail; 
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		 .detectDiskReads()
		 .detectDiskWrites()
		 .detectNetwork() // 这里可以替换为detectAll() 就包括了磁盘读写和网络I/O
		 .penaltyLog() //打印logcat
		 .build());
		 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
		 .detectLeakedSqlLiteObjects() //探测SQLite数据库操作
		 .penaltyLog() //打印logcat
		 .penaltyDeath()
		 .build()); 
		 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Toast.makeText(this.getApplicationContext(), "制作人：史守阳\n 欢迎光临我的博客：\n www.javaw.net",  
				 Toast.LENGTH_LONG).show();
		
		okButton = (Button) findViewById(R.id.ok);  

		  okButton.setOnClickListener(new Button.OnClickListener() {  
			  public void onClick(View v) {  
				 showWeather();  
			  }  
		  });  
		}  

		private void showWeather() {  
		  String city = "郑州";  
		  getWeather(city);  
		}  

		public void getWeather(String cityName) {  
		try {  
		 SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);  
		 rpc.addProperty("theCityName", cityName);  

		 HttpTransportSE ht = new HttpTransportSE (URL);  
		 ht.debug = true;  

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);  

		 envelope.bodyOut = rpc;  
		 envelope.dotNet = true;  
		 envelope.setOutputSoapObject(rpc);  

		  ht.call(SOAP_ACTION, envelope);  
//		  ht.call(null, envelope);  

		 SoapObject result = (SoapObject) envelope.bodyIn;  
		  detail = (SoapObject) result  
				  .getProperty("getWeatherbyCityNameResult");  

		  System.out.println("result" + result);  
		  System.out.println("detail" + detail);  
		  Toast.makeText(MainActivity.this, detail.toString(),  
				 Toast.LENGTH_LONG).show();  
//		  parseWeather(detail);  

		  return;  
		} catch (Exception e) {  
		  e.printStackTrace();  
		  }  
		}  

		private void parseWeather(SoapObject detail)  
			  throws UnsupportedEncodingException {  
		  String date = detail.getProperty(6).toString();  
		  weatherToday = "今天：" + date.split(" ")[0];  
		  weatherToday = weatherToday + "\n天气：" + date.split(" ")[1];  
		  weatherToday = weatherToday + "\n气温："  
				  + detail.getProperty(5).toString();  
		 weatherToday = weatherToday + "\n风力："  
				  + detail.getProperty(7).toString() + "\n";  
		  System.out.println("weatherToday is " + weatherToday);  
		  Toast.makeText(MainActivity.this, weatherToday, 
		  Toast.LENGTH_LONG).show();  

		 }  

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);

		return true;
	}
}
